/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mail;

import exceptions.GenericException;
import java.util.HashMap;
import java.util.Map;
import javax.mail.internet.MimeMessage;
import org.apache.velocity.app.VelocityEngine;

import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.stereotype.Controller;
import org.springframework.ui.velocity.VelocityEngineUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 *
 * @author tomasz
 */
@Controller
@RequestMapping("/szablon.htm")
public class szablon {
    
    private JavaMailSender mailSender;
   private VelocityEngine velocityEngine;

   public void setMailSender(JavaMailSender mailSender) {
      this.mailSender = mailSender;
   }

   public void setVelocityEngine(VelocityEngine velocityEngine) {
      this.velocityEngine = velocityEngine;
   }

    @RequestMapping(method = RequestMethod.GET)
    private String sendConfirmationEmail(final User user) 
    {
         MimeMessagePreparator preparator = new MimeMessagePreparator() {
                 public void prepare(MimeMessage mimeMessage) throws Exception {
                     
                    MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
                    message.setSubject("Poszło");
                    message.setTo("apap001@wp.pl");
                    message.setFrom("apap001@wp.pl"); // could be parameterized...
                    
                    User user = new User();
                    user.setEmailAddress("poszlo");
                    
                    Map model = new HashMap();
                    model.put("user", user);
                    
                    String text = VelocityEngineUtils.mergeTemplateIntoString(
                                            velocityEngine, "/registration-confirmation.vm", model);
                    
                    message.setText(text, true);
                    
                 }
         };

      
      this.mailSender.send(preparator);
      
      return "index";
      
   }

}
